package com.yuliang.ai.request.huoshanVideo;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpHeaders;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Huangkai
 */
@Slf4j
public class HuoShanVideo {

    public JSONObject requestVideo(String text){
        Map<String, String> header = new HashMap<>(2);
        header.put("X-Date", System.currentTimeMillis() /1000 + "");
        header.put("Content-Type", "application/json");
        header.put("X-Content-Sha256", System.currentTimeMillis() /1000 + "");
        header.put("Authorization", System.currentTimeMillis() /1000 + "");
        return null;
    }

    public String getAuthorization(){
        try{
            Credentials credentials = new Credentials();
            credentials.setAccessKeyID("your_ak");
            credentials.setSecretAccessKey("your_sk");
            credentials.setRegion("cn-north-1");
            credentials.setService("ic_iam");

            /* create signer */
            Signer signer = new Signer();

            /* create http client */
            CloseableHttpClient httpClient = HttpClients.createDefault();

            /* prepare request */
            HttpPost request = new HttpPost();
            request.setURI(new URI("https://open.volcengineapi.com?Action=ListUsers&Version=2022-02-01"));

            request.addHeader(HttpHeaders.USER_AGENT, "volc-sdk-java/v1.0.0");

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("UserType", "All"));
            //request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            signer.sign(request, credentials);

            /* launch request */
            CloseableHttpResponse response = httpClient.execute(request);

            /* status code */
            System.out.println(response.getStatusLine().getStatusCode());   // 200

            /* get response body */
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String result = EntityUtils.toString(entity);
                System.out.println(result);
            }

            /* close resources */
            response.close();
            httpClient.close();
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return null;
    }
}
